#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
	PROJECT pyevocomm -

	APP Parla

	MODULE Editor

	SITE DESCRIPTION http://code.google.com/p/pyevocomm/ 

	@copyright: 2012 by archetipo <alessio.gerace@gmail.com>
	@license: New BSD License, see COPYING for details.
"""


import sys
import os
import code

import pyglet
from pyglet import graphics
from pyglet import text
from pyglet.text import caret, document, layout

import cocos
from cocos.director import director
from cocos.layer import Layer, ColorLayer

__all__ = ['Editor']



class Editor(ColorLayer):

	cfg = {'t.font_name':'Arial',
			't.font_size':90,
			't.color':(30,30,30,255),
			'c.color':(255,30,30),
			}


	is_event_handler = True     #: enable pyglet's events

	def __init__(self):
		super(Editor, self).__init__( 32,32,32,192 )
		self.inizial=False
	
	def init(self,testo):
		""" Function doc

		@param PARAM: DESCRIPTION
		@return RETURN: DESCRIPTION
		"""
		
		self.content = testo
		vw,vh = cocos.director.director.get_window_size()
		self.posh=vh
		self.ratio=2.6
		self.edw=vw
		self.edh=int(vw/self.ratio)
	
		# format the code
		self.document = document.FormattedDocument(self.content)
		self.document.set_style(0, len(self.document.text), {
			'font_name': self.cfg['t.font_name'],
			'font_size': self.cfg['t.font_size'],
			'color': self.cfg['t.color'],
		})
		
		self.batch = graphics.Batch()

		# generate the document
		self.layout = layout.IncrementalTextLayout(self.document,
			self.edw, self.edh, multiline=True, batch=self.batch)
		self.layout.anchor_y= 'top'

		self.caret = caret.Caret(self.layout, color=self.cfg['c.color'] )
		self.caret.on_activate()

		self.on_resize(vw, vh)

		self.start_of_line = len(self.document.text)
		self.inizial=True

		
	def on_resize(self, x, y):
		vw, vh = director.get_window_size()
		self.layout.begin_update()
		self.layout.height = int(vw/self.ratio)
		self.layout.x = 2
		self.layout.width = vw - 4
		self.layout.y = vh
		self.layout.end_update()
		x,y = director.window.width, director.window.height
		self.layout.top_group._scissor_width=x-4

		self.caret.position = len(self.document.text)
		
	def delete(self, s):
		curr=len(self.document.text)
		if curr==1:
			self.inizial=False
		else:		
			self.document.delete_text(curr-1,curr)
			self._scroll_to_bottom()
				
	def write(self, s):
		self.document.insert_text(len(self.document.text), s, {
			'font_name': self.cfg['t.font_name'],
			'font_size': self.cfg['t.font_size'],
			'color': self.cfg['t.color'],
		})
		self._scroll_to_bottom()
	def _scroll_to_bottom(self):
		# on key press always move the view to the bottom of the screen
		if self.layout.height < self.layout.content_height:
			self.layout.anchor_y= 'bottom'
			self.layout.y = self.posh/4
			self.layout.view_y = 0
		self.caret.position = len(self.document.text)
            
	def on_exit(self):
		super(Editor, self).on_exit()
		self.content = self.document.text
		self.document = None
		self.layout = None
		self.batch = None
		self.caret = None

	def draw(self):
		super( Editor, self).draw()
		if self.inizial:self.batch.draw()
